package com.ruoyi.statistics.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistics.domain.StatisticsPay;
import com.ruoyi.statistics.mapper.StatisticsPayMapper;
import com.ruoyi.statistics.service.IStatisticsPayService;
import com.ruoyi.transaction.domain.TransactionPay;
import com.ruoyi.transaction.mapper.TransactionPayMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Service
public class StatisticsPayServiceImpl implements IStatisticsPayService 
{
    Logger log = LoggerFactory.getLogger(StatisticsPayServiceImpl.class);

	@Autowired
	private StatisticsPayMapper statisticsPayMapper;
	@Autowired
	private TransactionPayMapper transactionPayMapper;



	/**
     * 查询信息
     * 
     * @param id ID
     * @return 信息
     */
    @Override
	public StatisticsPay selectStatisticsPayById(Long id)
	{
	    return statisticsPayMapper.selectStatisticsPayById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param statisticsPay 信息
     * @return 集合
     */
	@Override
	public List<StatisticsPay> selectStatisticsPayList(StatisticsPay statisticsPay)
	{
	    return statisticsPayMapper.selectStatisticsPayList(statisticsPay);
	}




    /**
     * 新增
     * 
     * @param statisticsPay 信息
     * @return 结果
     */
	@Override
	public int insertStatisticsPay(StatisticsPay statisticsPay)
	{
	    return statisticsPayMapper.insertStatisticsPay(statisticsPay);
	}
	
	/**
     * 修改
     * 
     * @param statisticsPay 信息
     * @return 结果
     */
	@Override
	public int updateStatisticsPay(StatisticsPay statisticsPay)
	{
	    return statisticsPayMapper.updateStatisticsPay(statisticsPay);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteStatisticsPayByIds(String ids)
	{
		return statisticsPayMapper.deleteStatisticsPayByIds(Convert.toStrArray(ids));
	}


	/**
	 * 统计数据  按天
	 */
	@Override
	public boolean autoStatisticData(){
        try {
            // 查询支付记录所有渠道名称
            List<TransactionPay> payChannelNameList = transactionPayMapper.selectTransactionPayListWithChannelName();
            // 查询支付记录所有的商户名称
            List<TransactionPay> payMerchantNameList = transactionPayMapper.selectTransactionPayListWithMerchantName();
            // 查询支付记录所有的支付方式名称
            List<TransactionPay> payPayKindNameList = transactionPayMapper.selectTransactionPayListWithPayKindName();
            // 获取昨天的日期  2019-05-21
            String yesterday = getSelectDate(1);
            // 天
            statisticsByDay(yesterday);
            // 天    渠道名称
            statisticsByChannelName(payChannelNameList, yesterday);
            // 天    渠道名称 商户名称
            statisticsByChannelNameAndMerchantName(payChannelNameList, payMerchantNameList, yesterday);
            // 天    渠道名称 支付方式名称
            statisticsByChannelNameAndPayKindName(payChannelNameList, payPayKindNameList, yesterday);
            // 天    渠道名称 商户名称 支付方式名称
            statisticsByChannelNameAndMerchantNameAndPayKindName(payChannelNameList, payMerchantNameList, payPayKindNameList, yesterday);
            // 天	商户名称
            statisticsByMerchantName(payMerchantNameList, yesterday);
            // 天	商户名称	支付方式名称
            statisticsByMerchantNameAndPayKindName(payMerchantNameList, payPayKindNameList, yesterday);
            // 天   支付方式
            statisticsByPayKindName(payPayKindNameList, yesterday);

            // 统计记录
            // statisticsPay(payChannelNameList, payMerchantNameList, payPayKindNameList, yesterday);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("统计异常", e);
            return false;
        }
    }


    /**
     * 统计支付交易信息
     */
    public void statisticsPay(List<TransactionPay> payChannelNameList, List<TransactionPay> payMerchantNameList, List<TransactionPay> payPayKindNameList, String yesterday){
        for (TransactionPay channelPay : payChannelNameList) {
            for (TransactionPay merchantPay : payMerchantNameList) {
                for (TransactionPay payKindPay : payPayKindNameList) {
                    // 预期数据
                    TransactionPay transactionPayExpect = transactionPayMapper.statisticsByNameAndDate(channelPay.getChannelName(),merchantPay.getMerchantName(), payKindPay.getPayKindName(), yesterday);
                    // 实际数据
                    TransactionPay transactionPayReal = transactionPayMapper.statisticsByNameAndDateAndStatus(channelPay.getChannelName(),merchantPay.getMerchantName(), payKindPay.getPayKindName(), yesterday, 5);
                    // 记录数据
                    if (transactionPayExpect.getChFee() != null) {
                        recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                    }
                }
            }
        }
    }




    /**
     * 统计
     * 天
     */
    public void statisticsByDay(String yesterday){
        // 预期数据
        TransactionPay transactionPayExpect = transactionPayMapper.statisticsByDate(yesterday);
        // 实际数据
        TransactionPay transactionPayReal = transactionPayMapper.statisticsByDateAndStatus(yesterday, 5);
        // 记录数据
        if (transactionPayExpect.getChFee() != null) {
            recordSetStatisticsData(transactionPayExpect, transactionPayReal);
        }
    }




    /**
     * 统计
     * 天	渠道名称
     */
    public void statisticsByChannelName(List<TransactionPay> payList, String yesterday){
        if (payList.size() > 0) {
            for (TransactionPay transactionPay : payList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByChannelNameAndDate(transactionPay.getChannelName(),yesterday);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByChannelNameAndDateAndStatus(transactionPay.getChannelName(),yesterday, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }




    /**
     * 统计
     * 天	渠道名称	商户名称
     */
    public void statisticsByChannelNameAndMerchantName(List<TransactionPay> payChannelNameList, List<TransactionPay> payMerchantNameList, String yesterday) {
        if (payChannelNameList.size() > 0) {
            if (payMerchantNameList.size() > 0) {
                for (TransactionPay channelPay : payChannelNameList) {
                    for (TransactionPay merchantPay : payMerchantNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.selectPayListByChannelNameAndMerchantNameAndDay(channelPay.getChannelName(), merchantPay.getMerchantName(), yesterday);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.selectPayListByChannelNameAndMerchantNameAndDayAndStatus(channelPay.getChannelName(), merchantPay.getMerchantName(), yesterday, 5);
                        // 记录数据
                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }
    }


    /**
     * 统计
     * 天	渠道名称	支付方式名称
     */
    public void statisticsByChannelNameAndPayKindName(List<TransactionPay> payChannelNameList, List<TransactionPay> payPayKindNameList, String yesterday){
        if (payChannelNameList.size() > 0) {
            if (payPayKindNameList.size() > 0) {
                for (TransactionPay channelPay : payChannelNameList) {
                    for (TransactionPay payKindPay : payPayKindNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.selectPayListByChannelNameAndPayKindNameAndDay(channelPay.getChannelName(), payKindPay.getPayKindName(), yesterday);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.selectPayListByChannelNameAndPayKindNameAndDayAndStatus(channelPay.getChannelName(), payKindPay.getPayKindName(), yesterday, 5);
                        // 记录数据
                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }

    }


    /**
     * 统计
     * 天	渠道名称	商户名称 支付方式名称
     */
    public void statisticsByChannelNameAndMerchantNameAndPayKindName(List<TransactionPay> payChannelNameList, List<TransactionPay> payMerchantNameList, List<TransactionPay> payPayKindNameList, String yesterday) {
        if (payChannelNameList.size() > 0) {
            if (payMerchantNameList.size() > 0) {
                if (payPayKindNameList.size() > 0) {
                    for (TransactionPay channelPay : payChannelNameList) {
                        for (TransactionPay merhcantPay : payMerchantNameList) {
                            for (TransactionPay payKindPay : payPayKindNameList) {
                                // 预期数据
                                TransactionPay transactionPayExpect = transactionPayMapper.selectByChannelNameAndMerchantNameAndPaykindNameAndDay(channelPay.getChannelName(), channelPay.getMerchantName(), payKindPay.getPayKindName(), yesterday);
                                // 实际数据
                                TransactionPay transactionPayReal = transactionPayMapper.selectByChannelNameAndMerchantNameAndPaykindNameAndDayAndStatus(channelPay.getChannelName(), channelPay.getMerchantName(), payKindPay.getPayKindName(), yesterday, 5);
                                // 记录数据
                                if (transactionPayExpect.getChFee() != null) {
                                    recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 统计
     * 天	商户名称
     */
    public void statisticsByMerchantName(List<TransactionPay> payMerchantNameList, String yesterday) {
        if (payMerchantNameList.size() > 0) {
            for (TransactionPay merchantPay : payMerchantNameList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByMerchantNameAndDate(merchantPay.getMerchantName(),yesterday);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByMerchantNameAndDateAndStatus(merchantPay.getMerchantName(),yesterday, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }


    /**
     * 统一
     * 天	商户名称	支付方式名称
     */
    public void statisticsByMerchantNameAndPayKindName(List<TransactionPay> payMerchantNameList, List<TransactionPay> payPayKindNameList, String yesterday){
        if (payMerchantNameList.size() > 0) {
            if (payPayKindNameList.size() > 0) {
                for (TransactionPay merchantPay : payMerchantNameList) {
                    for (TransactionPay payKindPay : payPayKindNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.statisticsByMerchantNameAndPayKindNameAndDate(merchantPay.getMerchantName(),payKindPay.getPayKindName(),yesterday);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.statisticsByMerchantNameAndPayKindNameAndDateAndStatus(merchantPay.getMerchantName(),payKindPay.getPayKindName(),yesterday, 5);
                        // 记录数据

                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }
    }



    /**
     * 统计
     * 天	支付方式名称
     */
    public void statisticsByPayKindName(List<TransactionPay> payPayKindNameList, String yesterday) {
        if (payPayKindNameList.size() > 0) {
            for (TransactionPay payKindPay : payPayKindNameList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByPayKindNameAndDate(payKindPay.getPayKindName(),yesterday);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByPayKindNameAndDateAndStatus(payKindPay.getPayKindName(),yesterday, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsData(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }



    /**
     * 封装数据并记录   按天统计
     * @param transactionPayExpect   预期数据
     * @param transactionPayReal     实际数据
     */
    public void recordSetStatisticsData(TransactionPay transactionPayExpect, TransactionPay transactionPayReal) {
        // 统计记录
        StatisticsPay statisticsPay = new StatisticsPay();
        if (StringUtils.isNotBlank(transactionPayExpect.getChannelName())) {
            statisticsPay.setChannelId(transactionPayExpect.getChannelId());
            statisticsPay.setChannelName(transactionPayExpect.getChannelName());
            statisticsPay.setChannelCode(transactionPayExpect.getChannelCode());
        }else {
            statisticsPay.setChannelName("");
        }

        if (StringUtils.isNotBlank(transactionPayExpect.getMerchantName())) {
            statisticsPay.setMerchantId(transactionPayExpect.getMerchantId());
            statisticsPay.setMerchantName(transactionPayExpect.getMerchantName());
            statisticsPay.setMerchantCode(transactionPayExpect.getMerchantCode());
        }else {
            statisticsPay.setMerchantName("");
        }

        if (StringUtils.isNotBlank(transactionPayExpect.getPayKindName())) {
            statisticsPay.setPayKindId(transactionPayExpect.getPayKindId());
            statisticsPay.setPayKindName(transactionPayExpect.getPayKindName());
            statisticsPay.setPayKindCode(transactionPayExpect.getPayKindCode());
        }else {
            statisticsPay.setPayKindName("");
        }

        // 预期数据
        statisticsPay.setExpectTimes(transactionPayExpect.getMerchantNotifyTimes().longValue());
        statisticsPay.setExpectFee(transactionPayExpect.getSubFee());
        statisticsPay.setExpectChFee(transactionPayExpect.getChFee());
        statisticsPay.setExpectMchFee(transactionPayExpect.getMchFee());
        statisticsPay.setExpectFinFee(transactionPayExpect.getFinFee());
        // 实际数据
        if (transactionPayReal.getChFee() == null) {
            statisticsPay.setRealTimes(transactionPayReal.getMerchantNotifyTimes().longValue());
            statisticsPay.setRealFee(new BigDecimal("0.00"));
            statisticsPay.setRealChFee(new BigDecimal("0.00"));
            statisticsPay.setRealMchFee(new BigDecimal("0.00"));
            statisticsPay.setRealFinFee(new BigDecimal("0.00"));
        }else {
            statisticsPay.setRealTimes(transactionPayReal.getMerchantNotifyTimes().longValue());
            statisticsPay.setRealFee(transactionPayReal.getFee());
            statisticsPay.setRealChFee(transactionPayReal.getChFee());
            statisticsPay.setRealMchFee(transactionPayReal.getMchFee());
            statisticsPay.setRealFinFee(transactionPayReal.getFinFee());
        }
        // 插入时间
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        statisticsPay.setStatisticsTime(date);
        // 统计时间  昨天的数据   天
        Calendar calendarDateType = Calendar.getInstance();
        int dayDateType = calendarDateType.get(Calendar.DATE);
        calendarDateType.set(Calendar.DATE, dayDateType - 1);
        String dateDateType = new SimpleDateFormat("yyyy-MM-dd").format(calendarDateType.getTime());
        statisticsPay.setDateType(dateDateType);
        // 统计类型
        statisticsPay.setStatisticsType(1);
        // 插入数据
        statisticsPayMapper.insertStatisticsPay(statisticsPay);
    }


    /**
     * 获取日期    昨天  前7天    前一个月
     * @param count 查询天数
     * @return		日期
     */
    public String getSelectDate(int count){
        if (count > 1) {
            Calendar calendarOne = Calendar.getInstance();
            int dayOne = calendarOne.get(Calendar.DATE);
            calendarOne.set(Calendar.DATE, dayOne - 1);
            String dateOne = new SimpleDateFormat("yyyy-MM-dd").format(calendarOne.getTime());

            Calendar calendarTwo = Calendar.getInstance();
            int dayTwo = calendarTwo.get(Calendar.DATE);
            calendarTwo.set(Calendar.DATE, dayTwo - 1);
            String dateOTwo = new SimpleDateFormat("yyyy-MM-dd").format(calendarTwo.getTime());

            return dateOne + "~" + dateOTwo;
        }else {
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DATE);
            calendar.set(Calendar.DATE, day - 1);
            String date = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            return date;
        }
    }











    /*===================================================按小时===============================================================*/







    /**
     * 统计数据  按小时
     */
    @Override
    public boolean autoStatisticDataByHours() {
        try {
            // 查询支付记录所有渠道名称
            List<TransactionPay> payChannelNameList = transactionPayMapper.selectTransactionPayListWithChannelName();
            // 查询支付记录所有的商户名称
            List<TransactionPay> payMerchantNameList = transactionPayMapper.selectTransactionPayListWithMerchantName();
            // 查询支付记录所有的支付方式名称
            List<TransactionPay> payPayKindNameList = transactionPayMapper.selectTransactionPayListWithPayKindName();
            // 获取昨天的日期  时分秒
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            int day = cal.get(Calendar.DATE);
            cal.set(Calendar.DATE,day -1);
            String endTime = format.format(cal.getTime());
            cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)-1);
            String startTime = format.format(cal.getTime());
            // 天
            statisticsByHours(startTime, endTime);
            // 天    渠道名称
            statisticsByChannelNameAndHours(payChannelNameList, startTime, endTime);
            // 天    渠道名称 商户名称
            statisticsByChannelNameAndMerchantNameAndHours(payChannelNameList, payMerchantNameList, startTime, endTime);
            // 天    渠道名称 支付方式名称
            statisticsByChannelNameAndPayKindNameAndHours(payChannelNameList, payPayKindNameList, startTime, endTime);
            // 天    渠道名称 商户名称 支付方式名称
            statisticsByChannelNameAndMerchantNameAndPayKindNameAndHours(payChannelNameList, payMerchantNameList, payPayKindNameList, startTime, endTime);
            // 天	商户名称
            statisticsByMerchantNameAndHours(payMerchantNameList, startTime, endTime);
            // 天	商户名称	支付方式名称
            statisticsByMerchantNameAndPayKindNameAndHours(payMerchantNameList, payPayKindNameList, startTime, endTime);
            // 天   支付方式
            statisticsByPayKindNameAndHours(payPayKindNameList, startTime, endTime);

            // 统计记录
            // statisticsPayByHours(payChannelNameList, payMerchantNameList, payPayKindNameList, today);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("统计异常", e);
            return false;
        }
    }






    /**
     * 统计
     * 小时	渠道名称
     */
    public void statisticsByHours(String start, String end){
        // 预期数据
        TransactionPay transactionPayExpect = transactionPayMapper.statisticsByHours(start, end);
        // 实际数据
        TransactionPay transactionPayReal = transactionPayMapper.statisticsByDateAndStatusAndHours(start, end, 5);
        // 记录数据
        if (transactionPayExpect.getChFee() != null) {
            recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
        }
    }




    /**
     * 统计
     * 小时	渠道名称
     */
    public void statisticsByChannelNameAndHours(List<TransactionPay> payList, String start, String end){
        if (payList.size() > 0) {
            for (TransactionPay transactionPay : payList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByChannelNameAndHours(transactionPay.getChannelName(),start, end);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByChannelNameAndDateAndStatusAndHours(transactionPay.getChannelName(),start, end, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }




    /**
     * 统计
     * 小时	渠道名称	商户名称
     */
    public void statisticsByChannelNameAndMerchantNameAndHours(List<TransactionPay> payChannelNameList, List<TransactionPay> payMerchantNameList, String start, String end) {
        if (payChannelNameList.size() > 0) {
            if (payMerchantNameList.size() > 0) {
                for (TransactionPay channelPay : payChannelNameList) {
                    for (TransactionPay merchantPay : payMerchantNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.selectPayListByChannelNameAndMerchantNameAndHours(channelPay.getChannelName(), merchantPay.getMerchantName(), start, end);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.selectPayListByChannelNameAndMerchantNameAndHoursAndStatus(channelPay.getChannelName(), merchantPay.getMerchantName(), start, end, 5);
                        // 记录数据
                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }
    }


    /**
     * 统计
     * 小时	渠道名称	支付方式名称
     */
    public void statisticsByChannelNameAndPayKindNameAndHours(List<TransactionPay> payChannelNameList, List<TransactionPay> payPayKindNameList, String start, String end){
        if (payChannelNameList.size() > 0) {
            if (payPayKindNameList.size() > 0) {
                for (TransactionPay channelPay : payChannelNameList) {
                    for (TransactionPay payKindPay : payPayKindNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.selectPayListByChannelNameAndPayKindNameAndHours(channelPay.getChannelName(), payKindPay.getPayKindName(), start, end);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.selectPayListByChannelNameAndPayKindNameAndHoursAndStatus(channelPay.getChannelName(), payKindPay.getPayKindName(), start, end, 5);
                        // 记录数据
                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }

    }


    /**
     * 统计
     * 小时	渠道名称	商户名称 支付方式名称
     */
    public void statisticsByChannelNameAndMerchantNameAndPayKindNameAndHours(List<TransactionPay> payChannelNameList, List<TransactionPay> payMerchantNameList, List<TransactionPay> payPayKindNameList, String start, String end) {
        if (payChannelNameList.size() > 0) {
            if (payMerchantNameList.size() > 0) {
                if (payPayKindNameList.size() > 0) {
                    for (TransactionPay channelPay : payChannelNameList) {
                        for (TransactionPay merhcantPay : payMerchantNameList) {
                            for (TransactionPay payKindPay : payPayKindNameList) {
                                // 预期数据
                                TransactionPay transactionPayExpect = transactionPayMapper.selectByChannelNameAndMerchantNameAndPaykindNameAndHours(channelPay.getChannelName(), channelPay.getMerchantName(), payKindPay.getPayKindName(), start, end);
                                // 实际数据
                                TransactionPay transactionPayReal = transactionPayMapper.selectByChannelNameAndMerchantNameAndPaykindNameAndHoursAndStatus(channelPay.getChannelName(), channelPay.getMerchantName(), payKindPay.getPayKindName(), start, end, 5);
                                // 记录数据
                                if (transactionPayExpect.getChFee() != null) {
                                    recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 统计
     * 小时	商户名称
     */
    public void statisticsByMerchantNameAndHours(List<TransactionPay> payMerchantNameList, String start, String end) {
        if (payMerchantNameList.size() > 0) {
            for (TransactionPay merchantPay : payMerchantNameList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByMerchantNameAndHours(merchantPay.getMerchantName(),start, end);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByMerchantNameAndHoursAndStatus(merchantPay.getMerchantName(),start, end, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }


    /**
     * 统一
     * 小时	商户名称	支付方式名称
     */
    public void statisticsByMerchantNameAndPayKindNameAndHours(List<TransactionPay> payMerchantNameList, List<TransactionPay> payPayKindNameList, String start, String end){
        if (payMerchantNameList.size() > 0) {
            if (payPayKindNameList.size() > 0) {
                for (TransactionPay merchantPay : payMerchantNameList) {
                    for (TransactionPay payKindPay : payPayKindNameList) {
                        // 预期数据
                        TransactionPay transactionPayExpect = transactionPayMapper.statisticsByMerchantNameAndPayKindNameAndHours(merchantPay.getMerchantName(),payKindPay.getPayKindName(),start, end);
                        // 实际数据
                        TransactionPay transactionPayReal = transactionPayMapper.statisticsByMerchantNameAndPayKindNameAndHoursAndStatus(merchantPay.getMerchantName(),payKindPay.getPayKindName(),start, end, 5);
                        // 记录数据

                        if (transactionPayExpect.getChFee() != null) {
                            recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                        }
                    }
                }
            }
        }
    }



    /**
     * 统计
     * 小时	支付方式名称
     */
    public void statisticsByPayKindNameAndHours(List<TransactionPay> payPayKindNameList, String start, String end) {
        if (payPayKindNameList.size() > 0) {
            for (TransactionPay payKindPay : payPayKindNameList) {
                // 预期数据
                TransactionPay transactionPayExpect = transactionPayMapper.statisticsByPayKindNameAndHours(payKindPay.getPayKindName(),start, end);
                // 实际数据
                TransactionPay transactionPayReal = transactionPayMapper.statisticsByPayKindNameAndHoursAndStatus(payKindPay.getPayKindName(),start, end, 5);
                // 记录数据
                if (transactionPayExpect.getChFee() != null) {
                    recordSetStatisticsDataByHours(transactionPayExpect, transactionPayReal);
                }
            }
        }
    }



    /**
     * 封装数据并记录  按小时
     * @param transactionPayExpect   预期数据
     * @param transactionPayReal     实际数据
     */
    public void recordSetStatisticsDataByHours(TransactionPay transactionPayExpect, TransactionPay transactionPayReal) {
        // 统计记录
        StatisticsPay statisticsPay = new StatisticsPay();
        if (StringUtils.isNotBlank(transactionPayReal.getChannelName())) {
            statisticsPay.setChannelId(transactionPayReal.getChannelId());
            statisticsPay.setChannelName(transactionPayReal.getChannelName());
            statisticsPay.setChannelCode(transactionPayReal.getChannelCode());
        }else {
            statisticsPay.setChannelName("");
        }

        if (StringUtils.isNotBlank(transactionPayReal.getMerchantName())) {
            statisticsPay.setMerchantId(transactionPayReal.getMerchantId());
            statisticsPay.setMerchantName(transactionPayReal.getMerchantName());
            statisticsPay.setMerchantCode(transactionPayReal.getMerchantCode());
        }else {
            statisticsPay.setMerchantName("");
        }

        if (StringUtils.isNotBlank(transactionPayReal.getPayKindName())) {
            statisticsPay.setPayKindId(transactionPayReal.getPayKindId());
            statisticsPay.setPayKindName(transactionPayReal.getPayKindName());
            statisticsPay.setPayKindCode(transactionPayReal.getPayKindCode());
        }else {
            statisticsPay.setPayKindName("");
        }

        // 预期数据
        statisticsPay.setExpectTimes(transactionPayExpect.getMerchantNotifyTimes().longValue());
        statisticsPay.setExpectFee(transactionPayExpect.getSubFee());
        statisticsPay.setExpectChFee(transactionPayExpect.getChFee());
        statisticsPay.setExpectMchFee(transactionPayExpect.getMchFee());
        statisticsPay.setExpectFinFee(transactionPayExpect.getFinFee());
        // 实际数据
        // 实际数据
        if (transactionPayReal.getChFee() == null) {
            statisticsPay.setRealTimes(transactionPayReal.getMerchantNotifyStatus().longValue());
            statisticsPay.setRealFee(new BigDecimal("0.00"));
            statisticsPay.setRealChFee(new BigDecimal("0.00"));
            statisticsPay.setRealMchFee(new BigDecimal("0.00"));
            statisticsPay.setRealFinFee(new BigDecimal("0.00"));
        }else {
            statisticsPay.setRealTimes(transactionPayReal.getMerchantNotifyStatus().longValue());
            statisticsPay.setRealFee(transactionPayReal.getFee());
            statisticsPay.setRealChFee(transactionPayReal.getChFee());
            statisticsPay.setRealMchFee(transactionPayReal.getMchFee());
            statisticsPay.setRealFinFee(transactionPayReal.getFinFee());
        }
        // 插入时间
        // 获取昨天的日期  时分秒
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE,day);
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        statisticsPay.setStatisticsTime(date);
        // 统计时间  昨天的数据   小时区间
        String dateType = getDateType();
        statisticsPay.setDateType(dateType);
        // 统计类型
        statisticsPay.setStatisticsType(2);
        // 插入数据
        statisticsPayMapper.insertStatisticsPay(statisticsPay);
    }



    public String getDateType(){
        // 统计时间类型  昨天的数据   天
        Calendar calendarDateType = Calendar.getInstance();
        int dayDateType = calendarDateType.get(Calendar.DATE);
        calendarDateType.set(Calendar.DATE, dayDateType - 1);
        String day = new SimpleDateFormat("yyyy-MM-dd").format(calendarDateType.getTime());

        // 当前小时
        SimpleDateFormat formatHours = new SimpleDateFormat("HH:mm:ss");
        Calendar calCurHours = Calendar.getInstance();
        String CurHours = formatHours.format(calCurHours.getTime());

        // 上一个小时
        calCurHours.set(Calendar.HOUR_OF_DAY, calCurHours.get(Calendar.HOUR_OF_DAY)-1);
        String aftHours = formatHours.format(calCurHours.getTime());
        String dateType = day  + "   " + aftHours;

        return dateType;
    }
}
